package linhnv.app;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private static List<HashMap<String, String>> list ;
    private static HashMap<String, String> hotItem;
	private LocalActivityManager activityManager;
	private static ListItems listItem;
    public static ListItems getListItem() {
		return listItem;
	}

	public static void setListItem(ListItems listItem) {
		ImageAdapter.listItem = listItem;
	}

	public ImageAdapter(Context c, LocalActivityManager am) {
        mContext = c;
        Log.d("linhnv","============here i came 2");
        
        if(list == null)
        {
	        loadData();
	        Log.d("linhnv","============here i came 222" + list.toArray().length);
        }
        activityManager = am;
    }

    public int getCount() {   	
    	
        return list.size()-1;
       
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
    public HashMap<String, String> getHotItem()
    {
    	return hotItem;
    }
    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	View myView = convertView;
        if ( convertView == null )
        {
                               /*we define the view that will display on the grid*/
        //	Log.d("Linhnv","=======================================position:::"+position+"==========="+Constant.ROOT_ANDROID+ list.get(position).get("img").toString());
           //Inflate the layout
           LayoutInflater li =(LayoutInflater)mContext.getSystemService
           (Context.LAYOUT_INFLATER_SERVICE);
           myView = li.inflate(R.layout.grid_item, null);
           
           // Add The Text!!!
           TextView tv = (TextView)myView.findViewById(R.id.grid_item_text);
           tv.setText(list.get(position).get("name").toString());
           
           // Add The Image!!!           
           final ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image);
           iv.setImageBitmap(getImageBitmap(Constant.ROOT_ANDROID+ list.get(position).get("img").toString()));
           iv.setId(position);
           iv.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					AudioStreaming.frameTitleLayout.getLayoutParams().height = 0;
					
					int id = Integer.parseInt(list.get(iv.getId()).get("id").toString());
					String name = list.get(iv.getId()).get("name");
					
					if(ImageAdapter.getListItem() != null)
					{
						ImageAdapter.getListItem().show(id, name);
					}
					((Home) mContext).setHomeCurrentView(false);
					// TODO Auto-generated method stub
//					if(ImageAdapter.getListItem() == null)
//					{
						Intent intent = new Intent(mContext, ListItems.class);
						intent.putExtra("id", id);
						intent.putExtra("name", name);
						//intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
						
		            	View view = activityManager.startActivity("ShowItem", intent).getDecorView();
		            	((Home) mContext).setContentView(view);
//					}
//					else
//					{
//						ImageAdapter.getListItem().show(iv.getId());
//						((Home) mContext).setContentView(ImageAdapter.getListItem().getLayout());
//					}
	            	
				}
		});
           
        }
        
        return myView;
    }
    private Bitmap getImageBitmap(String url) { 
        Bitmap bm = null; 
        try { 
            URL aURL = new URL(url); 
            URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            InputStream is = conn.getInputStream(); 
            BufferedInputStream bis = new BufferedInputStream(is); 
            bm = BitmapFactory.decodeStream(bis); 
            bis.close(); 
            is.close(); 
       } catch (IOException e) { 
           Log.e("Linhnv", "Error getting bitmap", e); 
       } 
       return bm; 
    } 
    private void loadData()
    {
    	HttpCommunication httpComm = new HttpCommunication();
		
		// Set path for XML
    	httpComm.setUrl(Constant.CATEGORY);
    	InputStream inputStream = httpComm.get();
    	
    	if(null == inputStream){
    		ShowLog.showLogDebug("ThuNa", "InputStream from URL is null");
    	}
    	
    	ParseXMLAccess parserXML = new ParseXMLAccess();
    	parserXML.setDataSourceInputStream(inputStream, "category");
    	list = parserXML.getData();
    	
    	// Remove item that id = 25
    	for(int i = 0; i < list.size(); i ++)
    	{
    		if(list.get(i).get("id").equals("25")) // Id of hot
    		{
    			hotItem = list.get(i);
    			list.remove(i);
    			break;
    		}
    	}
    	//ShowLog.showLogDebug("Linhnv====", list.get(3).get("name").toString());
    	//Log.d("Linhnv","======================================="+Constant.ROOT+ list.get(3).get("img").toString());
    }
    
    
    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0,
            R.drawable.sample_0, R.drawable.sample_0
    };
}
